#!/usr/bin/env python

import requests, json

server = 'http://genome.ddns.comp.nus.edu.sg:8002'

def add_tag(id, tags):    
    addr = server + '/tags/add/' + str(id) + '/'
    r = requests.get(addr, params=tags)
    msg = json.loads(r.text)
    if (r.status_code != 200 or msg['status'] != 'OK'):
        print r.status_code
        print r.content
        raise Exception("Error")
    
def remove_tag(id, tags):
    addr = server + '/tags/remove/' + str(id) + '/'
    r = requests.get(addr, params=tags)
    msg = json.loads(r.text)
    if (r.status_code != 200 or msg['status'] != 'OK'):
        print r.status_code
        print r.content
        raise Exception("Error")

def find_by_tags(tags):
    #tags={'assembly':'hg19'}
    addr = server + '/find/'
    r = requests.get(addr, params=tags)

    msg = json.loads(r.text)
    if (r.status_code != 200 or msg['status'] != 'OK'):
        print r.status_code
        print r.content
        raise Exception("Error")
    return [src['id'] for src in msg['result']]

xlist = find_by_tags({'UCSC_name':''})
print xlist
for x in xlist:
    add_tag(x, {'data_origin': 'UCSC'})
    
#add_tag(find_by_tags({'assembly':'hg19'}), {'data_origin': 'UCSC'})